#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Person{
public:

char name[64];
int age;

};

void test01()
{
    ofstream ofs;
    ofs.open("out.txt", ofstream::out);

    ofs << "zhang3" << endl;
    ofs << "li4" << endl;

    ofs.close();
}

void test02()
{
    ifstream ifs;
    ifs.open("out.txt", ifstream::in);
    if (!ifs.is_open())
    {
        cout << "fail" << endl;
        return;
    }

    char buf[1024] = {0};
    // 一次读一行
    // while (ifs >> buf)
    // {
    //     cout<<"o"<<endl;
    //     cout << buf << endl;
    // }

// 一次读一行，超过限制了就跳过该行和它后面的所有行
    // while(ifs.getline(buf,5)){
    //     cout<<buf<<endl;
    // }

    // 用string对象，不限制长度了
    // string str;
    // while(getline(ifs,str)){
    //     cout<<str<<endl;
    // }

    char c;
    while((c=ifs.get())!=EOF){
        putchar(c);
    }

    ifs.close();
    cout<<"finish"<<endl;
}

void test03(){
    ofstream ofs;
    ofs.open("out.dat",ofstream::binary|ofstream::out);
    Person p={"zhang1234567890",18};
    ofs.write((const char *)&p,sizeof(Person));
    ofs.close();
}

void test04(){
    ifstream ifs("out.dat",ifstream::binary|ifstream::in);
    char buf[200];
    Person p={"",0};
    ifs.read((char *)&p,10);

cout<<p.name<<p.age<<endl;
    ifs.read((char *)&p,10);

cout<<p.name<<p.age<<endl;
}

int main()
{
    test01();
    test02();
    test03();
    test04();
    return 0;
}
